﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Composite.Regions;
using EQual.Infrastructure;

namespace EQual.MainModule.Employees.Trainings
{
    public class TrainingsController : ModulesControllerBase<TrainingsGridView  , DataAccesLayer.Training >
    {

        
        TrainingParticipantPresenter trainingPresenter;
        TrainingDetailsView detailsView;

        IRegionManager _regionManager;

        public TrainingsController(IRegionManager regionManager)
            : this()
        {
            this._regionManager = regionManager;
        }

        public TrainingsController()
        {
            this.AccesLayer = new DataAccesLayer.EQualDataLayerFactory();
            InitializeViews();
        }

        #region IModuleController Members

        public override  void Run()
        {
            View = new TrainingsGridView();
            detailsView = new TrainingDetailsView();


            TrainningsPresentationModel ViewModel  =  new TrainningsPresentationModel ();
            View.Model = ViewModel;

            trainingPresenter = new TrainingParticipantPresenter();

            View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemAdded);
            View.ItemSelected += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemSelected);
            this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => this.View);
            this._regionManager.Regions[RegionNames.MainRegion].Activate(this.View);
        }

        void View_ItemSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            this.ActiveItem = e.Value as DataAccesLayer .Training ;
        }

        void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            this.AccesLayer.GetEmployeeAccesLayer ().AddNewTraining(e.Value);
        }

        #endregion

        public override DataAccesLayer.Training ActiveItem
        {
            get
            {
                return base.ActiveItem;
            }
            set
            {
                base.ActiveItem = value;

            }
        }

        public override void Dispaly(DataAccesLayer.Training item)
        {
            InjectViews();

            this.trainingPresenter.ActiveItem   = item;
            this.detailsView.ActiveItem = item;

            base.Dispaly(item);
        }

        EQual.MainModule.DetailsContainer containerView = new DetailsContainer();

        protected virtual void InjectViews()
        {
            //throw in to the datails region the datails view
            IRegion region = _regionManager.Regions[RegionNames.ActionRegion];

            if (!region.Views.Contains(containerView))
            {
                containerView.AddView(GetDecoratedView( trainingPresenter .View , "Συμετέχοντες"));
                containerView.AddView(GetDecoratedView(detailsView, ""));

                region.Add(containerView);
            }

            //activate the current view
            region.Activate(containerView);
        }


    }
}
